<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\Goods as GoodsModel;

/**
 * 商城商品主管理
 *
 * @icon fa fa-circle-o
 */
class Goods extends Backend {

    /**
     * Goods模型对象
     * @var \app\admin\model\Goods
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\Goods;

        $this->view->assign("cateList", GoodsModel::getCateList());
        $this->view->assign("brandList", GoodsModel::getBrandList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $otherwhere[] = ['is_deleted', '=', 0];
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = collection($list)->toArray();
            if ($list) {
                //获取配置
//                $config_key = 'xiangguanpeizhi';
//                $configInfo = \think\Db::name('configure')->where(['key' => $config_key])->find();
//                $configValue = json_decode($configInfo['desc'],TRUE);
//                $val['referee_price'] = $val['finish_task_num'] * floatval($configValue['rakeback_rate']);
                foreach ($list as &$val) {
                    $val['brand_title'] = db('goods_brand')->where(['brand_id' => $val['brand_id']])->value('brand_title');
                    $val['cate_title'] = db('goods_cate')->where(['cate_id' => $val['cate_id']])->value('cate_title');
                }
                unset($val);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function up() {
        $goods_id = input('goods_id', 0, 'intval');
        $data['status'] = 1;
        $data['updatetime'] = date('Y-m-d H:i:s', time());
        db('goods')->where(['goods_id' => $goods_id])->update($data);
        $this->success();
    }

    public function down() {
        $goods_id = input('goods_id', 0, 'intval');
        $data['status'] = 0;
        $data['updatetime'] = date('Y-m-d H:i:s', time());
        db('goods')->where(['goods_id' => $goods_id])->update($data);
        $this->success();
    }

    public function delgoods() {
        $goods_id = input('goods_id', 0, 'intval');
        $data['is_deleted'] = 1;
        $data['updatetime'] = date('Y-m-d H:i:s', time());
        db('goods')->where(['goods_id' => $goods_id])->update($data);
        $hdata['is_del'] = 1;
        $hdata['updatetime'] = date('Y-m-d H:i:s', time());
        db('house_goods')->where(['goods_id' => $goods_id])->update($hdata);
        db('user_box_goods')->where(['goods_id' => $goods_id])->update($hdata);

        $user_box_goods_ids = db('user_box_goods')->where(['goods_id' => $goods_id])->column('user_box_id');

        db('cart')->where('user_box_id', 'in', $user_box_goods_ids)->delete();
        $this->success();
    }

    /**
     * 搜索下拉列表
     */
    public function brandList() {
        $brandList = db('goods_brand')->order('sort')->select();
        $list = [];
        foreach ($brandList as $key => $value) {
            $list[] = ['id' => $value['brand_id'], 'name' => $value['brand_title']];
        }
        $data = ['searchlist' => $list];
        $this->success('', null, $data);
    }

    /**
     * 搜索下拉列表
     */
    public function cateList() {
        $cateList = db('goods_cate')->order('sort')->select();
        $list = [];
        foreach ($cateList as $key => $value) {
            $list[] = ['id' => $value['cate_id'], 'name' => $value['cate_title']];
        }
        $data = ['searchlist' => $list];
        $this->success('', null, $data);
    }

}
